package com.itcast.utils;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/*
 工具类 数据库连接工具类
 */
public class ConnectionFactory {
    //创建一个datasource对象 自动获取c3po配置文件
    private static DataSource dataSource=new ComboPooledDataSource();
    //获取连接对象Connection
    public static Connection getConnection() throws SQLException {
        return  dataSource.getConnection();
    }
    public static void close(ResultSet resultSet, PreparedStatement preparedStatement,Connection connection)
    {
        try {
            if(resultSet!=null)
                resultSet.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if(preparedStatement!=null)
            preparedStatement.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if (connection!=null)
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


}
